﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameBase
{
    public static class ConfigsReader
    {
        public static void loadOptionConfig()
        {
            //          <optionConfig>
            //            <screenResolutions>
            //                <screenResolution>Full screen</screenResolution>
            //                <screenResolution>1278x700</screenResolution>
            //                <screenResolution>800x600</screenResolution>
            //            </screenResolutions>
            //            <fullscreen>true</fullscreen>
            //            <difficults>Easy,Normal,Hard,Insane</difficults>
            //          </optionConfig>

            try
            {
                // init xml reader
                XmlDocument doc = new XmlDocument();
                doc.Load(@"Config/optionConfigs.xml");
                XmlElement root = doc.DocumentElement;

                // load resolutions
                XmlElement eleScreenResolutions = (XmlElement)root.SelectSingleNode("//screenResolutions");
                XmlNodeList listEleScreenResolution = eleScreenResolutions.SelectNodes("//screenResolution");
                foreach (XmlNode node in listEleScreenResolution)
                {
                    Constant.resolutions.Add(node.InnerText);
                }

                // load full screen.
                XmlElement eleFullScreen = (XmlElement)root.SelectSingleNode("//fullscreen");
                Constant.isFullScreen = eleFullScreen.InnerText == @"true";

                // load difficults
                XmlElement eleDifficults = (XmlElement)root.SelectSingleNode("//difficults");
                string strDifficults = eleDifficults.InnerText;
                Constant.difficults = strDifficults.Split(',').ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: Can't load option config xml file: " + e.Message);
                throw (e);
            }
        }

        public static void loadGameConfig()
        {
            //  <gameConfig>
            //      <maxTeam>4</maxTeam>
            //      <hardLevel>0</hardLevel>
            //      <force>7</force>
            //      <decreaseForce>1</decreaseForce>
            //      <maxForce>20</maxForce>
            //      <timeForDisappear>30</timeForDisappear>
            //      <rangeRegenHealth>130</rangeRegenHealth>
            //      <regenTime>0.5</regenTime>
            //  </gameConfig>

            try
            {
                // init xml reader
                XmlDocument doc = new XmlDocument();
                doc.Load(@"Config/gameConfigs.xml");
                XmlElement root = doc.DocumentElement;

                // load maxTeam
                XmlElement maxTeam = (XmlElement)root.SelectSingleNode("//maxTeam");
                Constant.maxTeam = Int32.Parse(maxTeam.InnerText);

                // load hardLevel
                XmlElement hardLevel = (XmlElement)root.SelectSingleNode("//hardLevel");
                Constant.hardLevel = Int32.Parse(hardLevel.InnerText);

                // load force
                XmlElement force = (XmlElement)root.SelectSingleNode("//force");
                Constant.force = Int32.Parse(force.InnerText);

                // load decreaseForce
                XmlElement decreaseForce = (XmlElement)root.SelectSingleNode("//decreaseForce");
                Constant.decreaseForce = Int32.Parse(decreaseForce.InnerText);

                // load maxForce
                XmlElement maxForce = (XmlElement)root.SelectSingleNode("//maxForce");
                Constant.maxForce = Int32.Parse(maxForce.InnerText);

                // load timeForDisappear
                XmlElement timeForDisappear = (XmlElement)root.SelectSingleNode("//timeForDisappear");
                Constant.timeForDisappear = Int32.Parse(timeForDisappear.InnerText);

                // load rangeRegenHealth
                XmlElement rangeRegenHealth = (XmlElement)root.SelectSingleNode("//rangeRegenHealth");
                Constant.rangeRegenHealth = Int32.Parse(rangeRegenHealth.InnerText);

                // load regenTime
                XmlElement regenTime = (XmlElement)root.SelectSingleNode("//regenTime");
                Constant.regenTime = float.Parse(regenTime.InnerText);

                string[] screenResolution = Constant.resolutions[0].Split('x');
                if (screenResolution.Length == 2)
                {
                    Constant.screenWidth = Int32.Parse(screenResolution[0]);
                    Constant.screenHeight = Int32.Parse(screenResolution[1]);
                }

                Constant.graphics.IsFullScreen = Constant.isFullScreen;                
            }
            catch(Exception e)
            {
                Console.WriteLine("Error: Can't load game config xml file: " + e.Message);
                throw (e);
            }
        }

        public static void loadMapInfo()
        {

        }

        public static void applyResolution()
        {
            GameBase.Constant.graphics.PreferredBackBufferWidth = GameBase.Constant.screenWidth;
            GameBase.Constant.graphics.PreferredBackBufferHeight = GameBase.Constant.screenHeight;

            if (GameBase.Constant.isFullScreen)
                GameBase.Constant.graphics.IsFullScreen = true;
            else
                GameBase.Constant.graphics.IsFullScreen = false;

            GameBase.Constant.graphics.ApplyChanges();
        }
    }
}
